package org.yaac.server.util;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * @author Max Zhu (thebbsky@gmail.com)
 *
 */
public class FileUtil {

	/**
	 * convert an InputStream to a byte array
	 * 
	 * @param stream
	 * @return
	 * @throws IOException
	 */
	public static byte[] toByteArray(InputStream stream) throws IOException {
		byte[] rawData;
		int len;
		byte[] buffer = new byte[8192];
		ByteArrayOutputStream output = new ByteArrayOutputStream();

		try {
			while ((len = stream.read(buffer, 0, buffer.length)) != -1)
				output.write(buffer, 0, len);
			rawData = output.toByteArray();
		} finally {
			output.close();
		}
		return rawData;
	}
	
}
